#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <led.h>
#include "chuan.h"
#include "Serial.h"
uint8_t Serial2_RxData;
uint8_t Serial2_RxFlag;
uint8_t b;
extern flag;
extern eat;
void Serial2_Init(void)
{
    // ??USART2?GPIOA??(USART2?APB1???)
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure;
    // ??TX??(PA2)???????
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  // USART2_TX
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // ??RX??(PA3)?????
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;  // USART2_RX
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_Init(USART2, &USART_InitStructure);
    
    // ??USART2????
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;  // ??USART2??
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);
    
    USART_Cmd(USART2, ENABLE);
}

void Serial2_SendByte(uint8_t Byte)
{
    USART_SendData(USART2, Byte);
    while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}

void Serial2_SendArray(uint8_t *Array, uint16_t Length)
{
    uint16_t i;
    for (i = 0; i < Length; i ++)
    {
        Serial2_SendByte(Array[i]);
    }
}

void Serial2_SendString(char *String)
{
    uint8_t i;
    for (i = 0; String[i] != '\0'; i ++)
    {
        Serial2_SendByte(String[i]);
    }
}

uint32_t Serial2_Pow(uint32_t X, uint32_t Y)
{
    uint32_t Result = 1;
    while (Y --)
    {
        Result *= X;
    }
    return Result;
}

void Serial2_SendNumber(uint32_t Number, uint8_t Length)
{
    uint8_t i;
    for (i = 0; i < Length; i ++)
    {
        Serial2_SendByte(Number / Serial2_Pow(10, Length - i - 1) % 10 + '0');
    }
}

int fputc2																			(int ch, FILE *f)
{
    Serial2_SendByte(ch);
    return ch;
}

void Serial2_Printf(char *format, ...)
{
    char String[100];
    va_list arg;
    va_start(arg, format);
    vsprintf(String, format, arg);
    va_end(arg);
    Serial2_SendString(String);
}

uint8_t Serial2_GetRxFlag(void)
{
    if (Serial2_RxFlag == 1)
    {
        Serial2_RxFlag = 0;
        return 1;
    }
    return 0;
}

uint8_t Serial2_GetRxData(void)
{
    return Serial2_RxData;
}

void USART2_IRQHandler(void)  // ??USART2??????
{
    if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
    {
        Serial2_RxData = USART_ReceiveData(USART2);
        Serial2_RxFlag = 1;
        b = Serial2_RxData;
        Serial2_SendByte(b);
        
        unsigned char buf[64];
        sprintf((char *)buf, "page0.t5.txt=\"%d\"", b);
        HMISends((char *)buf);
        HMISendb(0xff);
        if(b == 'A')
        {
						LED_ON();
            flag = 1;
        }
        if(b == 'B')
        {
						LED_OFF();
            flag = 2;
        }
        if(b == 'C')
        {
            flag = 3;
        }
        if(b == 'D')
        {
            flag = 4;
        }
        if(b=='E')
				{
					eat=1;
				}
        sprintf((char *)buf, "page0.t2.txt=\"%d\"", flag);
        HMISends((char *)buf);
        HMISendb(0xff);
        
        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
    }
}